package com.lf.web.service.system.user;

import com.lf.data.Cluster;
import com.lf.data.ClusterUtil;
import com.lf.web.service.ServiceSupport;
import com.lf.web.service.system.role.RoleDAO;
import org.apache.log4j.Logger;

public class UserDetailService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(UserDetailService.class);
    private UserDAO userDAO;
    private RoleDAO roledao;
    private Cluster user;
    private Cluster role=new Cluster();
    private String id = "";

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void setRoledao(RoleDAO roledao) {
        this.roledao = roledao;
    }

    public String execute() {
        id = form.getString("id");
        LOGGER.debug("查看用户id=" + id);
        if (ClusterUtil.checkValue(User.class, "ID", id)) {
            user = userDAO.findById(id);
            if (user.isEmpty()) {
                LOGGER.warn("所查看的用户不存在：ID=" + id);
                error(404);
                return NONE;
            } else {
                if (user.containsKey(User.ROLE_ID)) {
                    String rid = user.getString(User.ROLE_ID);
                    role = roledao.findById(rid);
                }
            }
            putData();
            return "detail";
        } else {
            error(404);
            LOGGER.error("错误参数：id=" + id);
            return NONE;
        }
    }

    private void putData() {
        context.put("user", user);
        context.put("role", role);
        context.put("check", ClusterUtil.getCheck(User.class));
    }

}
